package com.cskaoyan.service;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketFootprint;
import com.cskaoyan.bean.po.MarketFootprintExample;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.vo.WxFootprintListInnerVO;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author zhuqiushi
 * @since 2022/09/09 10:59
 */
@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<WxFootprintListInnerVO> list(Integer page, Integer limit, Integer userId) {
        PageHelper.startPage(page, limit);
        //首先查footprint表
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        //加一个按时间排序
        marketFootprintExample.setOrderByClause("update_time desc");
        marketFootprintExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        CommonData result = CommonData.data(new PageInfo(marketFootprints));
        LinkedList<WxFootprintListInnerVO> wxFootprintListInnerVOS = new LinkedList<>();
        for (MarketFootprint marketFootprint : marketFootprints) {
            //根据footprint去goods表查信息
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
            WxFootprintListInnerVO target = new WxFootprintListInnerVO();
            BeanUtils.copyProperties(marketGoods, target);
            target.setId(marketFootprint.getId());
            target.setGoodsId(marketGoods.getId());
            target.setAddTime(marketFootprint.getAddTime());
            wxFootprintListInnerVOS.add(target);
        }
        result.setList(wxFootprintListInnerVOS);
        return result;
    }

    @Override
    public int delete(Integer id) {
        //直接根据id更改其deleted属性即可
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setId(id);
        marketFootprint.setDeleted(true);
        marketFootprintMapper.updateByPrimaryKeySelective(marketFootprint);
        return 0;
    }

}
